package com.itheima.service.impl;

import com.itheima.common.utils.DateUtils;
import com.itheima.mapper.OrdersettingMapper;
import com.itheima.pojo.OrderSetting;
import com.itheima.pojo.OrderSettingVo;
import com.itheima.service.OrdersettingService;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.time.LocalDate;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class OrdersettingServiceImpl implements OrdersettingService {

    @Autowired
    private OrdersettingMapper ordersettingMapper;

    //修改预约人数
    @Override
    public void editNumberByOrderDate(OrderSetting orderSetting) {

        String s = null;
        try {
            Date date = orderSetting.getOrderDate();
            s = DateUtils.parseDate2String(date);
            List<OrderSetting> byDate = ordersettingMapper.findByDate(s);

            if (byDate.size() == 0) {
                ordersettingMapper.editNumberByOrderDate(orderSetting);

            } else {

                ordersettingMapper.updateNumberByOrderDate(s, orderSetting.getNumber());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }


    }

    //根据年月查询对应的预约设置信息
    public List<OrderSettingVo> getOrderSettingByMonth(String month) {

        List<OrderSetting> orderSetting = ordersettingMapper.getOrderSettingByMonth(month);

        List<OrderSettingVo> orderSettingVo = new ArrayList<>();


        for (OrderSetting setting : orderSetting) {

            // 将 Date 转换为 LocalDate
            LocalDate orderDate = setting.getOrderDate().toInstant().atZone(ZoneId.systemDefault()).toLocalDate();


            int dayOfMonth = orderDate.getDayOfMonth();

            OrderSettingVo orderSettings = new OrderSettingVo();

            orderSettings.setDate(String.valueOf(dayOfMonth));
            orderSettings.setNumber(setting.getNumber());
            orderSettings.setReservations(setting.getReservations());
            orderSettingVo.add(orderSettings);
        }

        return orderSettingVo;
    }

    //上传预约设置信息 excle
    public void add(List<String[]> excel) throws Exception {


        for (String[] strings : excel) {


            System.out.println(strings.toString());
            OrderSetting orderSetting = new OrderSetting();

            if (strings[0] != null) {

                Date date = DateUtils.parseString2Date(strings[0], "yyyy/MM/dd");

                orderSetting.setOrderDate(date);
                if (strings[2] != null) {
                    orderSetting.setReservations(Integer.parseInt(strings[2]));
                }

                ordersettingMapper.add(orderSetting);
            }

        }
    }

    //下载预约设置信息
    public void download(String filename, HttpServletResponse response) {

        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setHeader("content-Disposition", "attachment;filename=" + filename);

        try {
            //读取模板文件
            InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("templates/" + filename);

            XSSFWorkbook excle = new XSSFWorkbook(inputStream);

            ServletOutputStream stream = response.getOutputStream();
            excle.write(stream);
        } catch (IOException e) {
            e.printStackTrace();
        }


    }
}



